﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Oplaco.Web.UI
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:RequiredFieldCategoryListBoxValidator runat=server></{0}:RequiredFieldCategoryListBoxValidator>")]
    public class RequiredFieldCategoryListBoxValidator : System.Web.UI.WebControls.BaseValidator 
    {
        private ListBox _control;

        /// <summary>
        /// Custom constructor
        /// </summary>
        public RequiredFieldCategoryListBoxValidator() {
            base.EnableClientScript = false;
        }

        /// <summary>
        /// Find the control to be validated
        /// </summary>
        /// <returns></returns>
        protected override bool ControlPropertiesValid()
        {
            try
            {
                Control control = FindControl(ControlToValidate);
                if (control == null) return false;
                else _control = (ListBox)control;            
            }
            catch (Exception exception) {
                Oplaco.Exceptions.AppService.ProcessAndNotifyPresentationLayerException(exception);
                return false;
            }
            return true;
        }

        /// <summary>
        /// Validate
        /// </summary>
        /// <returns></returns>
        protected override bool EvaluateIsValid()
        {
            if (int.Parse(_control.SelectedItem.Value) == -1) return false;
            return true;
        }
    }
}
